package tools.cyan.com.myapplication.ui;

import android.os.Bundle;

import com.cyan.tools.base.BaseListActivity;
import com.cyan.tools.base.BaseListView;
import com.cyan.tools.bean.Result;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import tools.cyan.com.myapplication.adapter.PhoneAdapter;
import tools.cyan.com.myapplication.app.App;
import tools.cyan.com.myapplication.bean.PhoneRecord;
import tools.cyan.com.myapplication.bean.Token;
import tools.cyan.com.myapplication.module.presenter.TestPresenterImpl;

public class MainActivity extends BaseListActivity implements BaseListView {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        App.getApp().getComponent().getAPIService().login("test","123456").subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Result<Token>>() {
            @Override
            public void accept(Result<Token> tokenResult) throws Exception {
                App.getApp().setToken(tokenResult.getData().getToken());
                fetch("",limit);
            }
        });
    }

    @Override
    protected void setup() {
        mPresenter = new TestPresenterImpl(this);
        adapter=new PhoneAdapter(this);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null)
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void fetch(String id, int limit) {
        mPresenter.fetch(id,limit);
    }

    @Override
    public String getId() {
        return String.valueOf(((PhoneRecord)(adapter.getItem(adapter.getCount()-1))).getId());
    }
}
